<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>检查类是否已定义</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.class-exists" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">class_exists</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">class_exists</span> &mdash; <span class="dc-title">检查类是否已定义</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.class-exists-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>class_exists</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$class_name</code></span>
   [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$autoload</code><span class="initializer"> = true</span></span>
  ] ) : <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   检查指定的类是否已定义。
  </p>
 </div>

 
 <div class="refsect1 parameters" id="refsect1-function.class-exists-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">class_name</code></dt>

     <dd>

      <p class="para">
       类名。名字的匹配是不分区大小写的。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">autoload</code></dt>

     <dd>

      <p class="para">
       是否默认调用 <a href="language.oop5.autoload.html" class="link">__autoload</a>。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.class-exists-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   如果由 <code class="parameter">class_name</code>
   所指的类已经定义，此函数返回 
   <strong><code>TRUE</code></strong>，否则返回 <strong><code>FALSE</code></strong>。
  </p>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.class-exists-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.0.2</td>
       <td>
        不再为已定义的 interface 返回 <strong><code>TRUE</code></strong>。请使用
        <span class="function"><a href="interface_exists.html" class="function">interface_exists()</a></span>。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.class-exists-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-6131">
    <p><strong>Example #1 <span class="function"><strong>class_exists()</strong></span>  例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;使用前检查类是否存在<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">class_exists</span><span style="color: #007700">(</span><span style="color: #DD0000">'MyClass'</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$myclass&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MyClass</span><span style="color: #007700">();<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
   <div class="example" id="example-6132">
    <p><strong>Example #2 <code class="parameter">autoload</code> parameter  例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">__autoload</span><span style="color: #007700">(</span><span style="color: #0000BB">$class</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;include(</span><span style="color: #0000BB">$class&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">'.php'</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Check&nbsp;to&nbsp;see&nbsp;whether&nbsp;the&nbsp;include&nbsp;declared&nbsp;the&nbsp;class<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(!</span><span style="color: #0000BB">class_exists</span><span style="color: #007700">(</span><span style="color: #0000BB">$class</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">trigger_error</span><span style="color: #007700">(</span><span style="color: #DD0000">"Unable&nbsp;to&nbsp;load&nbsp;class:&nbsp;</span><span style="color: #0000BB">$class</span><span style="color: #DD0000">"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">E_USER_WARNING</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />if&nbsp;(</span><span style="color: #0000BB">class_exists</span><span style="color: #007700">(</span><span style="color: #DD0000">'MyClass'</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$myclass&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MyClass</span><span style="color: #007700">();<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.class-exists-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function_exists.html" class="function" rel="rdfs-seeAlso">function_exists()</a> - 如果给定的函数已经被定义就返回 TRUE</span></li>
    <li class="member"><span class="function"><a href="interface_exists.html" class="function" rel="rdfs-seeAlso">interface_exists()</a> - 检查接口是否已被定义</span></li>
    <li class="member"><span class="function"><a href="get_declared_classes.html" class="function" rel="rdfs-seeAlso">get_declared_classes()</a> - 返回由已定义类的名字所组成的数组</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>